package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.AddressService;
import com.dongdongshop.vo.AddressVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("address")
@Controller
public class AddressController {
    @DubboReference
    private AddressService addressService;

    @RequestMapping("/listAddress")
    @ResponseBody
    public Result listAddress(){
        //获取购物车之前,获取用户的登录状态
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        String username = user.getUsername();
        List<AddressVo> list = addressService.getAddressByUserId(username);
        return Result.OK().setData(list);
    }
    @RequestMapping("/listProvinces")
    @ResponseBody
    public Result listProvinces(){
        List<TbProvinces> list = addressService.listProvinces();

        return Result.OK().setData(list);
    }
    @RequestMapping("/listCities")
    @ResponseBody
    public Result listCities(String provinceid){
        List<TbCities> list = addressService.listCities(provinceid);

        return Result.OK().setData(list);
    }
    @RequestMapping("/listArea")
    @ResponseBody
    public Result listArea(String cityid){
        List<TbAreas> list = addressService.listArea(cityid);

        return Result.OK().setData(list);
    }
    @RequestMapping("/addAddress")
    @ResponseBody
    public Result addAddress(TbAddress tbAddress){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        tbAddress.setUserId(user.getUsername());
        addressService.addAddress(tbAddress);
        return Result.OK();
    }
    @RequestMapping("/updateDef")
    @ResponseBody
    public Result updateDef(Long id,String userId){
        addressService.updateDef(id,userId);
        return Result.OK();
    }
    @RequestMapping("/toUpdate")
    @ResponseBody
    public Result toUpdate(Long id){
        TbAddress tbAddress = addressService.selectById(id);
        return Result.OK().setData(tbAddress);
    }
    @RequestMapping("/updateAddress")
    @ResponseBody
    public Result updateAddress(TbAddress tbAddress){
        addressService.updateAddress(tbAddress);
        return Result.OK();
    }
    @RequestMapping("/delAddress")
    @ResponseBody
    public Result delAddress(Long id){
        addressService.delAddress(id);
        return Result.OK();
    }
}
